package com.ujiuye.utils;

public class PageUtils {
    private int curPage;//当前页
    private int pageSize;//页容量
    private int nextPage;//下一页
    private int prevPage;//上一页
    private int rows;//总记录数
    private int endPage;//总页数
    private int startIndex;//每一页的起始行索引
    //总体初始化
    public PageUtils(int rows,String curPage) {
        this.rows=rows;
        this.pageSize=5;
        initCurPage(curPage);//初始化当前页
        initPrevPage();//初始化上一页
        initEndPage();//初始化总页码
        initNextPage();//初始化下一页
        initStartIndex();
    }

    //属性初始化
    //初始化当前页
    public void initCurPage(String curPage) {
        //当页面不传递当前页或者当前为空字符串的时候默认取值为1
        if (curPage == null || curPage.equals("")) {
            this.curPage = 1;
        } else {
            this.curPage = Integer.valueOf(curPage);
        }
    }

    //初始化上一页
    public void initPrevPage() {
        if (curPage > 1) {
            prevPage = curPage - 1;
        } else {
            prevPage = 1;
        }
    }

    //初始化下一页
    public void initNextPage() {
        if (curPage < endPage) {
            nextPage = curPage + 1;
        } else {
            nextPage = endPage;
        }
    }

    //初始化总页数
    public void initEndPage() {
        if (rows % pageSize == 0) {
            endPage = rows / pageSize;
        } else {
            endPage = rows / pageSize + 1;
        }
    }
    //初始化每页起始行索引
    public void initStartIndex(){
        startIndex = (curPage-1)*pageSize;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
}
